﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

namespace SkyReader
{
    public partial class Navigation : PhoneApplicationPage
    {
        private Book book;
        public Navigation()
        {
            InitializeComponent();

            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
            string fileName = settings["VARIABLE_CURRENTFILENAME"].ToString();

            foreach (var setting in settings)
            {
                if (setting.Value is Book)
                {
                    book = setting.Value as Book;
                    if (book.FileName == fileName)
                    {
                        break;
                    }
                }
            }
            this.lblCurrentPageNumber.Text = string.Format("{0}/{1}", book.CurrentPage, book.PageDict.Count);
        }

        private void GoButton_Click(object sender, EventArgs e)
        {
            int pageNumber=0;
            if (int.TryParse(this.txtPageNumber.Text, out pageNumber))
            {
                if (pageNumber > book.PageDict.Count)
                {
                    pageNumber = book.PageDict.Count;
                }
                if (pageNumber < 1)
                {
                    pageNumber = 1;
                }

                book.CurrentPage = pageNumber;
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("The page number must be integral.", "Invaild page number", MessageBoxButton.OK);
            }
            
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }
    }
}